import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:projectone/bean/books_bean.dart';
import './network_get_data.dart';
import 'book_detail_pages.dart';


// ignore: must_be_immutable
class BookTypePage extends StatefulWidget{
  BookTypeBean bookTypeBean;

  @override
  _BookTypeState createState() {
    // TODO: implement createState
    return _BookTypeState(bookTypeBean);
  }

  BookTypePage(this.bookTypeBean);

}
class _BookTypeState extends State<BookTypePage>{
  BookTypeBean bookTypeBean;
  _BookTypeState(this.bookTypeBean);

  //分类选择后，列表数据
  List<BooksTypeListBean> mList=new List();
  ScrollController _scrollController=new ScrollController();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      EasyLoading.show(status: 'loading...');
    });
    //页面加载，请求数据
    NetworkData().getBookListByType(bookTypeBean.typeUrl).then((value) =>
        setState(() {
          //EasyLoading.show(status: '数据加载中');
          mList = value;
          EasyLoading.dismiss();
        }));
    //增加控制器
    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        if(_page<5) _getMoreBook();
      }
    });
  }
  var _page=1;
  _getMoreBook(){
    _page++;
    EasyLoading.show(status: 'loading...');
    NetworkData().getBookListByType(bookTypeBean.typeUrl.replaceAll('.html', '/'+_page.toString()+'.html')).then((value) =>
        setState((){
          mList.addAll(value);
          EasyLoading.dismiss();
        })
    );
  }

  @override
  void dispose(){
    // 为了避免内存泄漏，需要调用_controller.dispose
    _scrollController.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build();
    return new Scaffold(
      appBar: new AppBar(
        title: Text(bookTypeBean.typeName+"小说"),
        centerTitle: true,
      ),
      body: ListView.builder(
            physics: const AlwaysScrollableScrollPhysics() ,
            itemCount: mList.length,
            controller: _scrollController,
            itemBuilder: (BuildContext context,int i){
              BooksTypeListBean temp=mList[i];
              return GestureDetector(
                child: Row(
                  children: <Widget>[
                    new Container(
                        padding: EdgeInsets.all(4),
                        child: new Image(
                            height: 120,
                            width: 90,
                            image: NetworkImage(temp.bookCover))),
                    new Expanded(
                      child: Container(
                        color: i%2==1?Colors.limeAccent:Colors.white,
                        child: new Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            new Text('《'+temp.bookName+'》'),
                            new Text('作者：'+temp.author),
                            new Divider(color: Colors.grey,height: 1,),
                            new Text(temp.bookIntro,maxLines: 2,overflow: TextOverflow.ellipsis,),
                            new Divider(color: Colors.grey,height: 1,),
                            new Text('最新章节:'+temp.lastTitle)
                          ],
                        ),
                      ),
                    )
                  ],
                ),
                onTap: (){
                    //跳转到图书章节位置
                    Navigator.push(context, MaterialPageRoute(builder: (context){
                      return BookInfoPage(temp.bookUrl,temp.bookName);
                    }));

                },
              );
            }),
    );
  }

}